<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    :width="400"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form :model="form" :rules="rules" ref="dialogForm" label-width="80px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="原单位">
            <skDicSelect v-model="form.firstunit_id" :code="'unit'"> </skDicSelect>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="系数">
            <el-input v-model="form.coefficient" :placeholder="'装换系数'"> </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="装换后单位">
            <skDicSelect v-model="form.lastunit_id" :code="'unit'"> </skDicSelect>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
import skDicSelect from "@/components/skDicSelect";
export default {
  emits: ["success", "closed"],
  components: { skDicSelect },
  data() {
    return {
      mode: "add",
      diccode: "CostType",
      titleMap: {
        add: "新增物料",
        edit: "编辑物料",
      },
      form: {},
      rules: {
        name: [{ required: true, message: "请输入名称" }],
        code: [{ required: true, message: "请输入编码" }],
      },
      visible: false,
      isSaveing: false,
    };
  },
  mounted() {},
  methods: {
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },

    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          var res = await this.$API.basedata.unit.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          } else {
            this.$alert(res.message, "提示", { type: "error" });
          }
        }
      });
    },
    //表单注入数据
    setData(data) {
      Object.assign(this.form, data);
    },
  },
};
</script>

<style></style>
